<?php

namespace App\Http\Controllers\Admin\Api;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Facades\ChargeRecordService;
use App\Facades\DeviceInfoService;
use App\Facades\DeviceService;
use App\Facades\UserService;
use Carbon\Carbon;
use DB;

class DeviceController extends Controller
{
    public function recharge(Request $request)
    {
    	$rules = [
            'flow' => 'required|integer',
    		'device_id' => 'required|exists:devices,id',
            'remark' => 'required',
    	];
    	$this->validate($request, $rules);

        DB::transaction(function() use ($request) {
        	$chargeRecord = ChargeRecordService::add($request->flow, $request->device_id, $request->remark);
            $deviceCode = $chargeRecord->device->code;
        	DeviceService::control('recharge', $request->flow, $deviceCode, $chargeRecord->id, $chargeRecord->user_id);
        });
    	return formatJsonResponse();
    }

    public function reduce(Request $request)
    {
        $rules = [
            'charge_record_id' => 'required|exists:charge_records,id',
            'flow' => 'required|integer',
            'device_id' => 'required|exists:devices,id',
            'remark' => 'required',
        ];
        $this->validate($request, $rules);
        
        DB::transaction(function() use ($request) {
            $user = UserService::hasPermission('finance');
            $chargeRecord = ChargeRecordService::updateChargeRecord($request->charge_record_id, ['status' => 3, 'discharge_admin_id' => $user->id, 'remark' => $request->remark, 'discharge_flow' => $request->flow]);
            $deviceCode = $chargeRecord->device->code;
            DeviceService::control('reduce_flow', $request->flow, $deviceCode, $chargeRecord->id);
        });
        return formatJsonResponse();
    }

    public function processFlowReminder(Request $request)
    {
        $rules = [
            'id' => 'required|exists:device_records,id',
            'flow_status' => 'required|boolean',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        $reminders = DeviceService::update($request->only(['id']), $request->only(['flow_status']));
        $count = DeviceService::countFlowRemindListByStatus(0);
        return formatJsonResponse(['count' => $count]);
    }

    public function processFilterReminder(Request $request)
    {
        $rules = [
            'id' => 'required|exists:device_records,id',
            'filter_status' => 'required|boolean',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        $reminders = DeviceService::update($request->only(['id']), $request->only(['filter_status']));
        $count = DeviceService::countFilterRemindListByStatus(0);
        return formatJsonResponse(['count' => $count]);
    }

    public function addInstallInfo(Request $request)
    {
        $rules = [
            'device_code' => 'required|digits:16',
            'customer_name' => 'required',
            'phone' => 'required|telephone',
            'installation_address' => 'required',
            'operator' => 'required',
            'operator_phone' => 'required|telephone',
            'status' => 'required|in:未安装,已安装',
            'dealer' => 'required',
            'install_at' => 'required|date_format:Y-m-d|after:2000-01-01',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        $installAt = Carbon::createFromFormat('Y-m-d', $request->install_at);
        DeviceInfoService::addInstallInfo(array_merge($request->only([
                                                            'device_code',
                                                            'customer_name',
                                                            'phone',
                                                            'installation_address',
                                                            'operator',
                                                            'operator_phone',
                                                            'status',
                                                            'dealer',
                                                            'install_at',
                                                            'remark',
                                                        ]), ['install_at' => $installAt]));
        return formatJsonResponse();
    }

    public function updateInstallInfo(Request $request)
    {
        $rules = [
            'id' => 'required|exists:install_infos,id',
            'device_code' => 'required|digits:16|unique:install_infos,device_code,'.$request->id,
            'customer_name' => 'required',
            'phone' => 'required|telephone',
            'installation_address' => 'required',
            'operator' => 'required',
            'operator_phone' => 'required|telephone',
            'status' => 'required|in:未安装,已安装',
            'dealer' => 'required',
            'install_at' => 'required|date_format:Y-m-d|after:2000-01-01',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        $installAt = Carbon::createFromFormat('Y-m-d', $request->install_at);
        DeviceInfoService::updateInstallInfo($request->id, array_merge($request->only([
                                                            'device_code',
                                                            'customer_name',
                                                            'phone',
                                                            'installation_address',
                                                            'operator',
                                                            'operator_phone',
                                                            'status',
                                                            'dealer',
                                                            'install_at',
                                                            'remark',
                                                        ]), ['install_at' => $installAt]));
        return formatJsonResponse();
    }

    public function deleteInstallInfo(Request $request)
    {
        $rules = [
            'id' => 'required|exists:install_infos,id',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        DeviceInfoService::deleteInstallInfo($request->id);
        return formatJsonResponse();
    }

    public function addMaintainInfo(Request $request)
    {
        $rules = [
            'device_code' => 'digits:16',
            'customer_name' => 'required',
            'phone' => 'required|telephone',
            'installation_address' => 'required',
            'charge' => 'numeric',
            'operator_phone' => 'telephone',
            'maintain_at' => 'date_format:Y-m-d|after:2000-01-01',
            'reserve_at' => 'required|date_format:Y-m-d H|after:2000-01-01 0',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        $maintainAt = Carbon::createFromFormat('Y-m-d', $request->maintain_at);
        DeviceInfoService::addMaintainInfo(array_merge($request->only([
                'device_code',
                'customer_name',
                'phone',
                'installation_address',
                'charge',
                'operator',
                'operator_phone',
                'component',
                'remark',
                'reserve_at',
            ]), ['maintain_at' => $maintainAt]));
        return formatJsonResponse();
    }

    public function updateMaintainInfo(Request $request)
    {
        $rules = [
            'id' => 'required|exists:maintain_infos,id',
            'device_code' => 'required|digits:16',
            'customer_name' => 'required',
            'phone' => 'required|telephone',
            'installation_address' => 'required',
            'charge' => 'required|numeric',
            'operator' => 'required',
            'operator_phone' => 'required|telephone',
            'component' => 'required',
            'maintain_at' => 'required|date_format:Y-m-d|after:2000-01-01',
            'reserve_at' => 'required|date_format:Y-m-d H|after:2000-01-01 0',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        $maintainAt = Carbon::createFromFormat('Y-m-d', $request->maintain_at);
        DeviceInfoService::updateMaintainInfo($request->id, array_merge($request->only([
                'device_code',
                'customer_name',
                'phone',
                'installation_address',
                'charge',
                'operator',
                'operator_phone',
                'component',
                'remark',
                'reserve_at',
            ]), ['maintain_at' => $maintainAt]));
        return formatJsonResponse();
    }

    public function getMaintainInfoById(Request $request)
    {
        $rules = [
            'id' => 'required|exists:maintain_infos,id',
        ];
        $this->validate($request, $rules);

        return formatJsonResponse(DeviceInfoService::getMaintainInfoById($request->id));
    }

    public function deleteMaintainInfo(Request $request)
    {
        $rules = [
            'id' => 'required|exists:maintain_infos,id',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission('admin');
        DeviceInfoService::deleteMaintainInfo($request->id);
        return formatJsonResponse();
    }

    public function halt(Request $request)
    {
        $rules = [
            'command' => 'required|in:halt,stop_halt',
            'device_code' => 'required|exists:devices,code',
        ];
        $this->validate($request, $rules);
        
        $user = UserService::hasPermission('finance');
        DeviceService::control($request->command, $request->num, $request->device_code, null,$user->id);
        return formatJsonResponse();
    }

    public function processMaintainInfo(Request $request)
    {
        $rules = [
            'id' => 'required|exists:maintain_infos,id',
            'status' => 'required|in:1',
        ];
        $this->validate($request, $rules);

        $user = UserService::hasPermission('finance');
        DeviceInfoService::updateMaintainInfo($request->id, $request->only(['status']));
        $count = DeviceService::countmaintainInfoListByStatus(0);
        return formatJsonResponse(['count' => $count]);
    }

    public function getInstallAddressByDeviceCode(Request $request)
    {
        $rules = [
            'device_code' => 'required|exists:install_infos,device_code',
        ];
        $this->validate($request, $rules);

        $installAddress = DeviceService::getInstallAddressByDeviceCode($request->device_code);
        return formatJsonResponse(['install_address' => $installAddress]);
    }


}
